package controller.db_manager;

import com.jfinal.aop.Before;
import com.jfinal.aop.ClearInterceptor;
import com.jfinal.kit.StrKit;
import common.MyBaseController;
import common.Util;

import java.lang.reflect.InvocationTargetException;

/**
 * Created by luob on 15/6/13.
 */
@Before(ManagerInterceptor.class)
public class IndexController extends MyBaseController {
    public void index() {
        render("admin-index-fixed.html");
    }

    public void dbview() {
        try {
            Class<?> m = Class.forName(getPara().replaceAll("\\*", "."));
            Object data = m.getDeclaredMethod("paginate", new Class[]{int.class, int.class}).invoke(m.getDeclaredField("me").get(null), 1, 10);
            setAttr("data", data);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        render("admin-view-table.html");
    }

    public void dbtable() {
        //System.out.println(getPara().replaceAll("\\*","."));
        try {
            Class<?> m = Class.forName(getPara().replaceAll("\\*", "."));
            //System.out.println(m);
            //System.out.println(m.getDeclaredField("me").get(null));
            //System.out.println(m.getDeclaredMethod("paginate", new Class[]{int.class, int.class}));
            Object data = m.getDeclaredMethod("paginate", new Class[]{int.class, int.class}).invoke(m.getDeclaredField("me").get(null), 1, 10);
            setAttr("data", data);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        render("admin-table.html");
    }

    public void dbtableadd() {
        if (!StrKit.isBlank(getPara("id"))) {
            renderError(10086);
            return;
        }
        String id = Util.randomUUID();
        try {
            Class<?> m = Class.forName(getPara().replaceAll("\\*", "."));
            //System.out.println(m);
            //System.out.println(m.getDeclaredField("me").get(null));
            //System.out.println(m.getDeclaredMethod("paginate", new Class[]{int.class, int.class}));
            Object md = m.newInstance();
            m.getMethod("set", new Class[]{String.class, Object.class}).invoke(md, "id", id);
            m.getMethod("save", new Class[]{}).invoke(md);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        redirect("/dbmanager/dbtableedit/" + getPara() + "?id=" + id);
    }

    public void dbtableedit() {
        //System.out.println(getPara());
        //System.out.println(getPara("id"));
        if (StrKit.isBlank(getPara("id"))) {
            renderError(10086);
            return;
        }
        try {
            Class<?> m = Class.forName(getPara().replaceAll("\\*", "."));
            //System.out.println(m);
            //System.out.println(m.getDeclaredField("me").get(null));
            //System.out.println(m.getDeclaredMethod("paginate", new Class[]{int.class, int.class}));
            Object data = m.getDeclaredMethod("paginate", new Class[]{int.class, int.class}).invoke(m.getDeclaredField("me").get(null), 1, 1);
            setAttr("data", data);
            Object mdata = m.getMethod("findById", new Class[]{Object.class}).invoke(m.getDeclaredField("me").get(null), new Object[]{getPara("id")});
            setAttr("mdata", mdata);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        render("admin-form-edit.html");
    }

    public void dbtabledelete() {
        if (!StrKit.isBlank(getPara("id"))) {
            setAttr("id", getPara("id"));
        }
        render("admin-form-delete.html");
    }

    @ClearInterceptor
    public void dbtabledeletebyid() {
        try {
            Class<?> m = Class.forName(getPara().replaceAll("\\*", "."));
            //System.out.println(m);
            //System.out.println(m.getDeclaredField("me").get(null));
            //System.out.println(m.getDeclaredMethod("paginate", new Class[]{int.class, int.class}));
            m.getMethod("delete", new Class[]{}).invoke(getModel(m, "m"), new Object[]{});
            addAttr("error", "0");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        handleResult();
    }

    @ClearInterceptor
    public void dbtableupdateattr() {
        try {
            Class<?> m = Class.forName(getPara().replaceAll("\\*", "."));
            //System.out.println(m);
            //System.out.println(m.getDeclaredField("me").get(null));
            //System.out.println(m.getDeclaredMethod("paginate", new Class[]{int.class, int.class}));
            m.getMethod("update", new Class[]{}).invoke(getModel(m, "m"), new Object[]{});
            addAttr("error", "0");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        handleResult();
    }
}
